package com.liarjo.my_website_api.admin.controller.messageboard;

import com.liarjo.my_website_api.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liarjo.my_website_api.admin.controller.messageboard.page.AdminMessageboardPageRequest;
import com.liarjo.my_website_api.admin.controller.messageboard.page.AdminMessageboardPageResult;

import java.util.Collection;

@RestController
@RequestMapping("/api/admin/messageboard")
public class AdminMessageboardController {
    @Autowired
    private AdminMessageboardService messageBoardService;
    @Autowired
    private SecurityUtil securityUtil;

    @GetMapping("/page")
    @PreAuthorize("hasAuthority('admin:messageboard:page')")
    public IPage<AdminMessageboardPageResult> page(AdminMessageboardPageRequest messageBoardPageRequest) {
        Collection<? extends GrantedAuthority> authorities = securityUtil.getAuthorities();
        for (GrantedAuthority authority : authorities) {
            System.out.println("User has authority: " + authority.getAuthority());
        }
        return messageBoardService.page(messageBoardPageRequest);
    }
}
